package maquina;

import gui.Debug;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;

import monitor.Host;
import monitor.MainMonitor;
import monitor.TipoOperacao;
import operacoes.GridMonitorOperation;
import operacoes.Mensagem;
import cliente.ClientSocket;

public class LocalProperties {
	
	//Componentes executando na mesma m�quina que o n� monitor
	Collection<Host> componentesMonitorados = new HashSet<Host>();
	private Properties props;
	public static double id;
	public static String filenamePeerUserList;
	public static String filenamePeerWorkersList;
	public static String filenameComponentsList;
	public static String filenameNodesList;
	public static String hostname;
	public static String IP;
	public static String grupoTrabalho;
	public static int portaExterna;
	public static int portaInterna;
	public static int fileTransferExternalPort;
	public static String separador = "#";
	public static int recuperacoesConsecutivas;
    public static String brokerPath;
    public static String peerPath;
    public static String workerPath;
    public static String openfirePath;
    public static int jobsPackageSize;
    public static int replicationjobsListAmount;
	
    public static void start(){
    	LocalProperties local = new LocalProperties();
    }
    
	public LocalProperties(){
		 String path = "/Users/jpaborges/Documents/workspace/GridMonitor/hamster.properties";
		 //String path = "/Users/rafaelfarias/workspace/Hamster/hamster.properties";
		 boolean solicitarPath = true;
		 props = new Properties();  
		 FileInputStream fis = null;  
		 try {  
			 while (solicitarPath){
				 try{
					 fis = new FileInputStream(new File(path));
					 solicitarPath = false;
				 }catch (FileNotFoundException ex) {  
					 BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));
					 System.out.printf("O path %s nao foi encontrado.\nDigite o path do hamster.properties:\n",path);
					 path = teclado.readLine();
				 }
			 }
		     
		     props.load(fis);    
		     fis.close();
		     
		     //Filling the properties...
			 portaExterna=Integer.parseInt(props.getProperty("externalPort"));  
			 portaInterna=Integer.parseInt(props.getProperty("internalPort"));  
			 fileTransferExternalPort=Integer.parseInt(props.getProperty("fileTransferExternalPort"));
			 
			 //IP=InetAddress.getLocalHost().getHostAddress();  
			 IP=props.getProperty("ip"); 
			 
			 recuperacoesConsecutivas=Integer.parseInt(props.getProperty("successiveReconnections"));  
			 
			 grupoTrabalho= props.getProperty("workgroup");  
			 
			 filenamePeerUserList= props.getProperty("filenamePeerUserList");  
			 filenamePeerWorkersList= props.getProperty("filenamePeerWorkersList");  
			 filenameComponentsList = props.getProperty("filenameComponentsList");  
			 filenameNodesList= props.getProperty("filenameNodesList");
			 
			 brokerPath=props.getProperty("brokerPath");
			 peerPath=props.getProperty("peerPath");
			 workerPath=props.getProperty("workerPath");
			 openfirePath=props.getProperty("openfirePath");
		     jobsPackageSize=Integer.parseInt(props.getProperty("jobsPackageSize"));
		     replicationjobsListAmount=Integer.parseInt(props.getProperty("replicationjobsListAmount"));
			 
		 } catch (IOException ex) {  
		     System.out.println(ex.getMessage());  
		     ex.printStackTrace();  
		 } 
	}
	
	public static void main(String args[]){
		new LocalProperties();
	}
	
	public void inserirNovoComponente(Host host){
		//Verifica entre os hosts conhecidos, se a porta de comunicação com o novo componente já está sendo utilizada por outros...
		for (Host hostInterno : componentesMonitorados) {
			//Se a porta já estiver sendo utilizada...
			if(hostInterno.getPorta() == host.getPorta()){
				//Envia mensagem para o componente solicitante, informando a sua nova porta...
				try {
					ClientSocket.enviarMSG("127.0.0.1", 9000, Mensagem.montarMensagem(TipoOperacao.HEARTBEAT+""));
				} catch (IOException e) {
					//String[] mensagem = {"","",Maquina.tipoMonitorado+"","", TipoOperacao.RECOVERY+""};
					String[] mensagem = {"","","","", TipoOperacao.RECOVERY+""};
					GridMonitorOperation gmo = new GridMonitorOperation(mensagem, MainMonitor.externo);
					//GridMonitorOperation.processRequest(mensagem);
					gmo.run();
					Debug.println(e.getMessage());
				}
				break;
			} else {
				//Adiciona o novo componente na lista de monitoráveis...
				componentesMonitorados.add(host);
			}
		}
	}
	
}
